package com.fps.web.controller.user;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fps.web.controller.BaseController;
import com.fps.web.model.BaseUser;
import com.fps.web.service.BaseUserService;
import com.fpswork.core.model.PageEntity;
import com.fpswork.util.StringUtil;


/**
 * 登录页面
    * @date 2015-11-19
    *
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
    
    @Autowired
    BaseUserService baseUserService;
    
    
   
    /** 
	 * 加载List数据
	 */
    @RequestMapping(value = "/loadUserList", method = RequestMethod.GET)
    public @ResponseBody PageEntity loadUserList(PageEntity pageEntity) throws Exception{
    	baseUserService.queryUserList(pageEntity);
        return pageEntity;
    }
    
    /**
   	 * 用户管理
   	 */
    @RequestMapping("/userManagement")
    public ModelAndView userManagement(String id,String type) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();   
    	BaseUser baseuser= new BaseUser();
    	baseuser = baseUserService.queryUserByID(id);
    	modelAndView.addObject("baseuser",baseuser);
    	modelAndView.addObject("type", type);
		modelAndView.setViewName("web/user/usermanagement");      
        return modelAndView;
    }
    
    /**
   	 * 用户管理提交表单
   	 */
    @RequestMapping(value = "/userManagementSubmit", method = {RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody Map<String, String> userManagementSubmit(@ModelAttribute("BaseUser")BaseUser baseUser) throws Exception{
    	if(StringUtil.isEmpty(baseUser.getId()))
    	baseUser.setPassword(StringUtil.toMD5(baseUser.getPassword()));
    	baseUserService.addUser(baseUser);
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("flag", "true");
        return map;
    }
    
    /**
   	 * 用户删除
   	 */
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public @ResponseBody Map<String, String> deleteUser(@ModelAttribute("BaseUser")BaseUser baseUser) throws Exception{
    	baseUserService.deleteUser(baseUser);
    	Map<String, String> map = new HashMap<String, String>();
		map.put("flag", "true");
        return map;
    }
    

}
